<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>拖动左侧边栏</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    body {
      display: flex;
      height: 100vh;
    }
    .sidebar {
      position: relative;
      min-width: 200px;
      height: 100%;
      background-color: aquamarine;
    }
    .senarator {
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      right: 0;
      height: 100%;
      width: 20px;
      background-color: #fff;
      cursor: col-resize;
    }
    .main {
      width: 0;
      flex: 1;
      background-color: orange;
    }
    .sidebar .senarator i {
      width: 2px;
      height: 20px;
      background-color: #e9e9e9;
      margin-right: 4px;
    }
  </style>
</head>
<body>
  <!-- 左侧边栏 -->
  <div class="sidebar">
    <!-- 用来调整拖动 -->
    <div class="senarator">
      <i></i>
      <i></i>
    </div>
  </div>
  <div class="main"></div>
</body>
<script>
  let senarator = document.querySelector('.senarator');
  let sidebar = document.querySelector('.sidebar');
  let startX, startWidth;

  function getWidth() {
    return parseInt(window.getComputedStyle(sidebar).width);
  }

  senarator.addEventListener('mousedown', function(e) {
    startX = e.clientX;
    startWidth = getWidth();
    document.documentElement.addEventListener('mousemove', onDrag);
    document.documentElement.addEventListener('mouseup', onStopDrag);
  });

  function onDrag(e) {
    let newWidth = e.clientX - startX + startWidth;
    sidebar.style.width = `${newWidth}px`;
  };

  function onStopDrag() {
    document.documentElement.removeEventListener('mousemove', onDrag);
    document.documentElement.removeEventListener('mouseup', onStopDrag);
  }
</script>
</html>